<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.3.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">


<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"example.com","root":"/","scheme":"Muse","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.json"};
  </script>

  <meta name="description" content="​      原文链接：https:&#x2F;&#x2F;blog.csdn.net&#x2F;qq_38410730&#x2F;article&#x2F;details&#x2F;89921413 TIM+ADC+DMA原理​        一般情况下，当我们需要进行采样的时候，需要用到ADC。例如：需要对某个信号进行定时采样（也就是隔一段时间，比如说2ms）。 ​        本文提供的解决方案是：使用ADC的定时器触发ADC单次转换的功能，然后使">
<meta property="og:type" content="article">
<meta property="og:title" content="STM32定时器TIM触发ADC采样DMA搬运到内存">
<meta property="og:url" content="http://example.com/2024/04/17/STM32%E5%AE%9A%E6%97%B6%E5%99%A8TIM%E8%A7%A6%E5%8F%91ADC%E9%87%87%E6%A0%B7DMA%E6%90%AC%E8%BF%90%E5%88%B0%E5%86%85%E5%AD%98/index.html">
<meta property="og:site_name" content="学无止境">
<meta property="og:description" content="​      原文链接：https:&#x2F;&#x2F;blog.csdn.net&#x2F;qq_38410730&#x2F;article&#x2F;details&#x2F;89921413 TIM+ADC+DMA原理​        一般情况下，当我们需要进行采样的时候，需要用到ADC。例如：需要对某个信号进行定时采样（也就是隔一段时间，比如说2ms）。 ​        本文提供的解决方案是：使用ADC的定时器触发ADC单次转换的功能，然后使">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/20190507154706603.png">
<meta property="og:image" content="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/201905071549347.png">
<meta property="og:image" content="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/%E5%BE%AE%E4%BF%A1%E6%88%AA%E5%9B%BE_20240417150824.png">
<meta property="article:published_time" content="2024-04-17T07:00:12.000Z">
<meta property="article:modified_time" content="2024-04-17T07:00:12.000Z">
<meta property="article:author" content="xudonglei">
<meta property="article:tag" content="stm32">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/20190507154706603.png">

<link rel="canonical" href="http://example.com/2024/04/17/STM32%E5%AE%9A%E6%97%B6%E5%99%A8TIM%E8%A7%A6%E5%8F%91ADC%E9%87%87%E6%A0%B7DMA%E6%90%AC%E8%BF%90%E5%88%B0%E5%86%85%E5%AD%98/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>STM32定时器TIM触发ADC采样DMA搬运到内存 | 学无止境</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">学无止境</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
      <p class="site-subtitle" itemprop="description">Do not bb, show me the example</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="menu">
        <li class="menu-item menu-item-home">

    <a href="/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/tags/" rel="section"><i class="fa fa-fw fa-tags"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/categories/" rel="section"><i class="fa fa-fw fa-th"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://example.com/2024/04/17/STM32%E5%AE%9A%E6%97%B6%E5%99%A8TIM%E8%A7%A6%E5%8F%91ADC%E9%87%87%E6%A0%B7DMA%E6%90%AC%E8%BF%90%E5%88%B0%E5%86%85%E5%AD%98/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="xudonglei">
      <meta itemprop="description" content="搬砖 嵌入式">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="学无止境">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          STM32定时器TIM触发ADC采样DMA搬运到内存
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2024-04-17 15:00:12" itemprop="dateCreated datePublished" datetime="2024-04-17T15:00:12+08:00">2024-04-17</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E5%B5%8C%E5%85%A5%E5%BC%8F/" itemprop="url" rel="index"><span itemprop="name">嵌入式</span></a>
                </span>
            </span>

          
            <span id="/2024/04/17/STM32%E5%AE%9A%E6%97%B6%E5%99%A8TIM%E8%A7%A6%E5%8F%91ADC%E9%87%87%E6%A0%B7DMA%E6%90%AC%E8%BF%90%E5%88%B0%E5%86%85%E5%AD%98/" class="post-meta-item leancloud_visitors" data-flag-title="STM32定时器TIM触发ADC采样DMA搬运到内存" title="阅读次数">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span class="leancloud-visitors-count"></span>
            </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="fa fa-comment-o"></i>
      </span>
      <span class="post-meta-item-text">Valine：</span>
    
    <a title="valine" href="/2024/04/17/STM32%E5%AE%9A%E6%97%B6%E5%99%A8TIM%E8%A7%A6%E5%8F%91ADC%E9%87%87%E6%A0%B7DMA%E6%90%AC%E8%BF%90%E5%88%B0%E5%86%85%E5%AD%98/#valine-comments" itemprop="discussionUrl">
      <span class="post-comments-count valine-comment-count" data-xid="/2024/04/17/STM32%E5%AE%9A%E6%97%B6%E5%99%A8TIM%E8%A7%A6%E5%8F%91ADC%E9%87%87%E6%A0%B7DMA%E6%90%AC%E8%BF%90%E5%88%B0%E5%86%85%E5%AD%98/" itemprop="commentCount"></span>
    </a>
  </span>
  
  

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>​      原文链接：<a target="_blank" rel="noopener" href="https://blog.csdn.net/qq_38410730/article/details/89921413">https://blog.csdn.net/qq_38410730/article/details/89921413</a></p>
<h3 id="TIM-ADC-DMA原理"><a href="#TIM-ADC-DMA原理" class="headerlink" title="TIM+ADC+DMA原理"></a>TIM+ADC+DMA原理</h3><p>​        一般情况下，当我们需要进行采样的时候，需要用到ADC。例如：需要对某个信号进行定时采样（也就是隔一段时间，比如说2ms）。</p>
<p>​        本文提供的解决方案是：<strong>使用ADC的定时器触发ADC单次转换的功能，然后使用DMA进行数据的搬运！</strong></p>
<p>​        这样只要设置好定时器的触发间隔，就能实现ADC定时采样转换的功能（即采样速率），然后可以在程序的死循环中一直检测DMA转换完成标志，然后进行数据的读取，或者使能DMA转换完成中断，这样每次转换完成就会产生中断。</p>
<p>​        主要需要解决的一个问题：<strong>定时器触发ADC采样，如何实现？</strong></p>
<p>​        定时器触发ADC采样，是属于外部触发转换的一种方式。在《STM32中文参考手册》中，找到了关于这部分的内容：<br><img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/20190507154706603.png" style="zoom:80%;"></p>
<p>配合上ADC外设的框图：</p>
<img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/201905071549347.png" style="zoom:77%;">

<p>​        可以看出，STM32的ADC1和ADC2用于规则通道的外部触发可以有以上6个事件信号，本文使用TIM2_CH2触发ADC1。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T2_CC2;  <span class="comment">//使用外部触发模式</span></span><br><span class="line">ADC_ExternalTrigConvCmd(ADC1, ENABLE);		<span class="comment">//设置外部触发模式使能</span></span><br></pre></td></tr></table></figure>
<p>​        同时注意一下外部触发的触发条件：<strong>当外部触发信号被选为ADC规则或注入转换时，只有它的上升沿可以启动转换</strong>。</p>
<p>​        如何有上升沿呢？定时器配置为<strong>PWM输出模式</strong>，这是重点。通过调用<code>TIM_OC2Init(Tim2, &amp; TIM_OCInitStructure)</code>，完成对<code>TIM2_CH2</code>的PWM配置。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;		<span class="comment">//选择定时器模式:TIM脉冲宽度调制模式1</span></span><br><span class="line">TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;		<span class="comment">//比较输出使能</span></span><br><span class="line">TIM_OCInitStructure.TIM_Pulse = <span class="number">1000</span>;</span><br><span class="line">TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;		<span class="comment">//输出极性:TIM输出比较极性低</span></span><br><span class="line">TIM_OC2Init(TIM2, &amp; TIM_OCInitStructure);		<span class="comment">//初始化外设TIM2_CH2</span></span><br></pre></td></tr></table></figure>


<p>其次，就是<strong>DMA将采样的数据由ADC1外设搬运到内存中</strong>。</p>
<p>配置DMA的外设地址和内存地址，并设置方向为从外设到内存即可。</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;				<span class="comment">//ADC1地址</span></span><br><span class="line">DMA_InitStructure.DMA_MemoryBaseAddr = (<span class="keyword">uint32_t</span>)&amp;ADC_ConvertedValue; 		<span class="comment">//内存地址</span></span><br><span class="line">DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; 				<span class="comment">//方向(从外设到内存)</span></span><br></pre></td></tr></table></figure>


<h3 id="STM32全部源码"><a href="#STM32全部源码" class="headerlink" title="STM32全部源码"></a>STM32全部源码</h3><p>​        本文采用的外设为：<strong>TIM2_CH2外部触发PA6（ADC1_CH6）采样，通过DMA1搬运到内存。</strong></p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span> <span class="meta-string">&quot;adc.h&quot;</span></span></span><br><span class="line"></span><br><span class="line"><span class="keyword">volatile</span> <span class="keyword">uint16_t</span> ADC_ConvertedValue;			<span class="comment">//ADC采样的数据</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> ADC1_DR_Address    ((u32)0x4001244C)		<span class="comment">//ADC1的地址</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">//TIM2配置，arr为重加载值，psc为预分频系数</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">TIM2_Init</span><span class="params">(u16 arr,u16 psc)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;</span><br><span class="line">	TIM_OCInitTypeDef TIM_OCInitStructure;</span><br><span class="line">	</span><br><span class="line">	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); 		<span class="comment">//时钟使能</span></span><br><span class="line"></span><br><span class="line">	<span class="comment">//定时器TIM2初始化</span></span><br><span class="line">	TIM_TimeBaseStructure.TIM_Period = arr; 		<span class="comment">//设置在下一个更新事件装入活动的自动重装载寄存器周期的值</span></span><br><span class="line">	TIM_TimeBaseStructure.TIM_Prescaler =psc; 			<span class="comment">//设置用来作为TIMx时钟频率除数的预分频值</span></span><br><span class="line">	TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; 		<span class="comment">//设置时钟分割:TDTS = Tck_tim</span></span><br><span class="line">	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; 		<span class="comment">//TIM向上计数模式</span></span><br><span class="line">	TIM_TimeBaseInit(TIM2, &amp;TIM_TimeBaseStructure);			<span class="comment">//根据指定的参数初始化TIMx的时间基数单位</span></span><br><span class="line">	</span><br><span class="line">	TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;		<span class="comment">//选择定时器模式:TIM脉冲宽度调制模式1</span></span><br><span class="line">	TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;		<span class="comment">//比较输出使能</span></span><br><span class="line">	TIM_OCInitStructure.TIM_Pulse = <span class="number">1000</span>;</span><br><span class="line">	TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;		<span class="comment">//输出极性:TIM输出比较极性低</span></span><br><span class="line">	TIM_OC2Init(TIM2, &amp; TIM_OCInitStructure);		<span class="comment">//初始化外设TIM2_CH2</span></span><br><span class="line">	</span><br><span class="line">	TIM_Cmd(TIM2, ENABLE); 			<span class="comment">//使能TIMx</span></span><br><span class="line">	TIM_CtrlPWMOutputs(TIM2, ENABLE); </span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//DMA1配置</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">DMA1_Init</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	DMA_InitTypeDef DMA_InitStructure;</span><br><span class="line">	NVIC_InitTypeDef NVIC_InitStructure;</span><br><span class="line"></span><br><span class="line">	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);	  			<span class="comment">//使能ADC1通道时钟</span></span><br><span class="line">	</span><br><span class="line">	<span class="comment">//DMA1初始化</span></span><br><span class="line">	DMA_DeInit(DMA1_Channel1);</span><br><span class="line">	DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;				<span class="comment">//ADC1地址</span></span><br><span class="line">	DMA_InitStructure.DMA_MemoryBaseAddr = (<span class="keyword">uint32_t</span>)&amp;ADC_ConvertedValue; 		<span class="comment">//内存地址</span></span><br><span class="line">	DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; 				<span class="comment">//方向(从外设到内存)</span></span><br><span class="line">	DMA_InitStructure.DMA_BufferSize = <span class="number">1</span>; 						<span class="comment">//传输内容的大小</span></span><br><span class="line">	DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; 		<span class="comment">//外设地址固定</span></span><br><span class="line">	DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable; 			<span class="comment">//内存地址固定</span></span><br><span class="line">	DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord ; <span class="comment">//外设数据单位</span></span><br><span class="line">	DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord ;    <span class="comment">//内存数据单位</span></span><br><span class="line">	DMA_InitStructure.DMA_Mode = DMA_Mode_Circular  ; 		<span class="comment">//DMA模式：循环传输</span></span><br><span class="line">	DMA_InitStructure.DMA_Priority = DMA_Priority_High ; 		<span class="comment">//优先级：高</span></span><br><span class="line">	DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;   		<span class="comment">//禁止内存到内存的传输</span></span><br><span class="line">	DMA_Init(DMA1_Channel1, &amp;DMA_InitStructure);  <span class="comment">//配置DMA1</span></span><br><span class="line">	</span><br><span class="line">	DMA_ITConfig(DMA1_Channel1,DMA_IT_TC, ENABLE);		<span class="comment">//使能传输完成中断</span></span><br><span class="line"></span><br><span class="line">	NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel1_IRQn;</span><br><span class="line">	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = <span class="number">0</span>;</span><br><span class="line">	NVIC_InitStructure.NVIC_IRQChannelSubPriority = <span class="number">0</span>;</span><br><span class="line">	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;</span><br><span class="line">	NVIC_Init(&amp;NVIC_InitStructure);</span><br><span class="line">	</span><br><span class="line">	DMA_Cmd(DMA1_Channel1,ENABLE);</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//GPIO配置，PA6</span></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">GPIO_Init</span><span class="params">()</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	GPIO_InitTypeDef GPIO_InitStructure;</span><br><span class="line">	</span><br><span class="line">	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);	  <span class="comment">//使能GPIOA时钟</span></span><br><span class="line"></span><br><span class="line">	<span class="comment">//PA6 作为模拟通道输入引脚   </span></span><br><span class="line">	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;</span><br><span class="line">	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;</span><br><span class="line">	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;</span><br><span class="line">	GPIO_Init(GPIOA, &amp;GPIO_InitStructure);  </span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">void</span> <span class="title">Adc_Init</span><span class="params">()</span></span>&#123;</span><br><span class="line">	ADC_InitTypeDef ADC_InitStructure;</span><br><span class="line">	</span><br><span class="line">	TIM2_Init(<span class="number">30000</span>,<span class="number">7199</span>);		<span class="comment">//72000000/7200=10000Hz，每3s采集一次</span></span><br><span class="line">	DMA1_Init();</span><br><span class="line">	GPIO_Init();</span><br><span class="line"></span><br><span class="line">	RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);	  <span class="comment">//使能ADC1通道时钟</span></span><br><span class="line"></span><br><span class="line">	<span class="comment">//ADC1初始化</span></span><br><span class="line">	ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; 			<span class="comment">//独立ADC模式</span></span><br><span class="line">	ADC_InitStructure.ADC_ScanConvMode = DISABLE;  			<span class="comment">//关闭扫描方式</span></span><br><span class="line">	ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;			<span class="comment">//关闭连续转换模式</span></span><br><span class="line">	ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T2_CC2;   	<span class="comment">//使用外部触发模式</span></span><br><span class="line">	ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; 			<span class="comment">//采集数据右对齐</span></span><br><span class="line">	ADC_InitStructure.ADC_NbrOfChannel = <span class="number">1</span>; 			<span class="comment">//要转换的通道数目</span></span><br><span class="line">	ADC_Init(ADC1, &amp;ADC_InitStructure);</span><br><span class="line">	</span><br><span class="line">	RCC_ADCCLKConfig(RCC_PCLK2_Div6);				<span class="comment">//配置ADC时钟，为PCLK2的6分频，即12Hz</span></span><br><span class="line">	ADC_RegularChannelConfig(ADC1, ADC_Channel_6, <span class="number">1</span>, ADC_SampleTime_239Cycles5);		<span class="comment">//配置ADC1通道6为239.5个采样周期 </span></span><br><span class="line">	</span><br><span class="line">	<span class="comment">//使能ADC、DMA</span></span><br><span class="line">	ADC_DMACmd(ADC1,ENABLE);</span><br><span class="line">	ADC_Cmd(ADC1,ENABLE);</span><br><span class="line"> </span><br><span class="line">	ADC_ResetCalibration(ADC1);				<span class="comment">//复位校准寄存器</span></span><br><span class="line">	<span class="keyword">while</span>(ADC_GetResetCalibrationStatus(ADC1));				<span class="comment">//等待校准寄存器复位完成</span></span><br><span class="line"> </span><br><span class="line">	ADC_StartCalibration(ADC1);				<span class="comment">//ADC校准</span></span><br><span class="line">	<span class="keyword">while</span>(ADC_GetCalibrationStatus(ADC1));				<span class="comment">//等待校准完成</span></span><br><span class="line">	</span><br><span class="line">	ADC_ExternalTrigConvCmd(ADC1, ENABLE);		<span class="comment">//设置外部触发模式使能</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//中断处理函数</span></span><br><span class="line"><span class="function"><span class="keyword">void</span>  <span class="title">DMA1_Channel1_IRQHandler</span><span class="params">(<span class="keyword">void</span>)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">	<span class="keyword">if</span>(DMA_GetITStatus(DMA1_IT_TC1)!=RESET)&#123;</span><br><span class="line">		<span class="comment">//中断处理代码</span></span><br><span class="line">    	<span class="built_in">printf</span>(<span class="string">&quot;The current value =%d \r\n&quot;</span>,ADC_ConvertedValue);</span><br><span class="line">		DMA_ClearITPendingBit(DMA1_IT_TC1);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>​        主程序中只需要调用Adc_Init()，然后空循环即可。此时串口调试助手，就会每隔3秒把ADC_ConvertedValue的值打印出来了。</p>
<p>​        <strong>另外需要注意一个知识点：</strong></p>
<p><img src="https://xdl-blog-picture.oss-cn-shanghai.aliyuncs.com/img/%E5%BE%AE%E4%BF%A1%E6%88%AA%E5%9B%BE_20240417150824.png"></p>

    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/stm32/" rel="tag"># stm32</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/2024/04/10/memos%E6%95%B0%E6%8D%AE%E5%BA%93%E6%96%87%E4%BB%B6%E5%AE%9A%E6%97%B6%E5%A4%87%E4%BB%BD%E5%88%B0gitee/" rel="prev" title="memos数据库文件定时备份到gitee">
      <i class="fa fa-chevron-left"></i> memos数据库文件定时备份到gitee
    </a></div>
      <div class="post-nav-item">
    <a href="/2024/04/21/%E5%8D%A1%E5%B0%94%E6%9B%BC%E6%BB%A4%E6%B3%A2%E4%B8%AD%E9%83%A8%E5%88%86%E7%9F%A9%E9%98%B5%E7%9A%84%E7%90%86%E8%A7%A3/" rel="next" title="卡尔曼滤波中部分矩阵的理解">
      卡尔曼滤波中部分矩阵的理解 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          
    <div class="comments" id="valine-comments"></div>

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#TIM-ADC-DMA%E5%8E%9F%E7%90%86"><span class="nav-number">1.</span> <span class="nav-text">TIM+ADC+DMA原理</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#STM32%E5%85%A8%E9%83%A8%E6%BA%90%E7%A0%81"><span class="nav-number">2.</span> <span class="nav-text">STM32全部源码</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">xudonglei</p>
  <div class="site-description" itemprop="description">搬砖 嵌入式</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">283</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">19</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">67</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2024</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">xudonglei</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://muse.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js"></script>
  <script src="/lib/velocity/velocity.min.js"></script>
  <script src="/lib/velocity/velocity.ui.min.js"></script>

<script src="/js/utils.js"></script>

<script src="/js/motion.js"></script>


<script src="/js/schemes/muse.js"></script>


<script src="/js/next-boot.js"></script>




  




  
<script src="/js/local-search.js"></script>













  

  


<script>
NexT.utils.loadComments(document.querySelector('#valine-comments'), () => {
  NexT.utils.getScript('//unpkg.com/valine/dist/Valine.min.js', () => {
    var GUEST = ['nick', 'mail', 'link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item => {
      return GUEST.includes(item);
    });
    new Valine({
      el         : '#valine-comments',
      verify     : false,
      notify     : false,
      appId      : 'BT0Qw9HcTpPv1a6cT1iDBXKz-gzGzoHsz',
      appKey     : 'LMCMh8v02JTMFAwKNRcgoizI',
      placeholder: "Just go go",
      avatar     : 'mm',
      meta       : guest,
      pageSize   : '10' || 10,
      visitor    : true,
      lang       : 'zh-CN' || 'zh-cn',
      path       : location.pathname,
      recordIP   : false,
      serverURLs : ''
    });
  }, window.Valine);
});
</script>

</body>
</html>
